package io.renren.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.entity.*;
import io.renren.service.GoodshelvesService;
import io.renren.service.TakeoutsetService;
import io.renren.service.ProductService;
import io.renren.service.ProductcategoryService;
import io.renren.service.ShopcartService;
import io.renren.service.ReplenishmentService;
import io.renren.service.ProductshelvesstcokService;
import io.renren.service.ReplenishmentitemService;
import io.renren.common.utils.R;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/api/short")
public class ShortController {
	@Autowired
	ProductshelvesstcokService productStcokService;
	@Autowired
	ProductService productService;
	@Autowired
	ProductcategoryService productcategoryService;
	@Autowired
	GoodshelvesService goodshelvesService;
	@Autowired
	ShopcartService shopcartServices;
	@Autowired
	TakeoutsetService takeoutsetService;
	@Autowired
	ReplenishmentService replenishmentService;
	@Autowired
	ReplenishmentitemService replenishmentitemService;

	/**
	 * 获取指定货架的货架商品
	 * @param shelvesId 货架id
	 * @return
	 */
	@RequestMapping("/shelvesInfo")
	@ResponseBody
	public R shelvesInfo(@RequestParam("shelvesId")Integer shelvesId) {

		if(shelvesId!=null){
			//当前货架
			GoodshelvesEntity goodshelves = goodshelvesService.getById(shelvesId);
			if(goodshelves!=null){
				//货架商品
				List<ProductEntity> listgood = productStcokService.getProductByShelvesId(shelvesId);
				//分类
				List<Integer> catIds = new ArrayList<>();
				if(listgood!=null&&listgood.size()>0) {
					for (ProductEntity product : listgood) {
						String categoryid = product.getCategoryid();
						if(StringUtils.isNotBlank(categoryid)){
							String[] categoryArray = categoryid.split(",");
							for (int i = 0; i < categoryArray.length; i++) {
								int parseInt = Integer.parseInt(categoryArray[i]);
								if(!catIds.contains(parseInt)){
									catIds.add(parseInt);
								}

							}
						}else{
							if(!catIds.contains(0)){
								catIds.add(0);
							}
						}

					}
				}

				JSONArray array = new JSONArray();
				if(catIds.size()>0){
					for(Integer id:catIds){
						JSONObject json = new JSONObject();
						if(id ==0){
							json.put("shelvesCatgegory","未分组");
						}else{
							ProductcategoryEntity productcategoryEntity = productcategoryService.getById(id);
							json.put("shelvesCatgegory",productcategoryEntity.getName());
						}

						List<ProductEntity> productlist = productService.getProductList(id,shelvesId);

						if(productlist!=null && productlist.size()>0){
							JSONArray jsonArray = new JSONArray();
							for(ProductEntity product:productlist){

								JSONObject jsonobject = new JSONObject();
								jsonobject.put("productid",product.getId());
								jsonobject.put("productimg",product.getProductimg());
								jsonobject.put("productname",product.getProductname());
								jsonobject.put("sellprice",product.getSellprice());
								jsonArray.add(jsonobject);
							}
							json.put("prodcuts",jsonArray);
						}

						array.add(json);
					}
				}

				return R.ok().put("shelvesname",goodshelves.getShelvesname()).put("data",array);
			}else{
				return R.error("所查询的货架不存在");
			}

		}else{
			return R.error("货架id不能为空");
		}

	}


	/**
	 * 加入购物车
	 * @param userid 购买人id
	 * @param productid 商品id
	 * @param count 商品数量
	 * @return
	 */
	@RequestMapping("/addshopcart")
	@ResponseBody
	public R addshopcart(@RequestParam("userid")Integer userid,@RequestParam("productid")Integer productid,@RequestParam("count")Integer count) {
		if(userid ==null){
			return R.error("购买人id不能为空");
		}else if(productid ==null){
			return R.error("商品id不能为空");
		}else if(count ==null){
			return R.error("购买商品数量不能为空");
		}else{
			ShopcartEntity shopcartEntity = shopcartServices.getProductInShopCart(userid,productid);
			if(shopcartEntity!=null){//该购买人的购物车里有该商品，修改改商品数量
				shopcartEntity.setCount(shopcartEntity.getCount()+count);
				shopcartServices.updateById(shopcartEntity);
			}else{
				shopcartEntity = new ShopcartEntity();
				shopcartEntity.setCount(count);
				shopcartEntity.setCreatetime(new Date());
				shopcartEntity.setProductid(productid);
				shopcartEntity.setUserid(userid);
				ProductEntity productEntity = productService.getById(productid);
				shopcartEntity.setTotalprice(productEntity.getSellprice().multiply(new BigDecimal(count)).toString());
			}
		}

		return R.ok();
	}


	/**
	 * 购物车信息
	 * @param userid 购买者id
	 * @return
	 */
	@RequestMapping("/shopcart")
	@ResponseBody
	public R shopcart(@RequestParam("userid")Integer userid) {
		JSONArray array = new JSONArray();
		JSONObject json = new JSONObject();
		if(userid ==null){
			return R.error("购买人id不能为空");
		}else{
			List<ShopcartEntity> list = shopcartServices.getShopCartInfoByUserId(userid);
			BigDecimal price = new BigDecimal(0);
			if(list!=null && list.size()>0){
				for(ShopcartEntity se:list){
					JSONObject jsonobject = new JSONObject();

					ProductEntity product = productService.getById(se.getProductid());
					jsonobject.put("productid",se.getProductid());
					jsonobject.put("productimg",product.getProductimg());
					jsonobject.put("productname",product.getProductname());
					jsonobject.put("productprice",product.getSellprice().multiply(new BigDecimal(se.getCount())));
					jsonobject.put("count",se.getCount());
					array.add(jsonobject);
					price = price.add(product.getSellprice().multiply(new BigDecimal(se.getCount())));
				}
			}
			json.put("totalprice",price.toString());
			json.put("products",array);
		}

		return R.ok().put("data",json);
	}


	/**
	 * 获取指定货架的外卖商品信息
	 * @param shelvesId 货架id
	 * @return
	 */
	@RequestMapping("/takeoutshelvesInfo")
	@ResponseBody
	public R takeoutshelvesInfo(@RequestParam("shelvesId")Integer shelvesId) {

		if(shelvesId!=null){
			//当前货架
			GoodshelvesEntity goodshelves = goodshelvesService.getById(shelvesId);
			if(goodshelves!=null){
				TakeoutsetEntity takeoutsetEntity = takeoutsetService.queryListByShelvesid(shelvesId);
				if(takeoutsetEntity.getStatu() == 0){
					//货架商品
					List<ProductEntity> listgood = productStcokService.getProductByShelvesId(shelvesId);
					//分类
					List<Integer> catIds = new ArrayList<>();
					if(listgood!=null&&listgood.size()>0) {
						for (ProductEntity product : listgood) {
							String categoryid = product.getCategoryid();
							if(StringUtils.isNotBlank(categoryid)){
								String[] categoryArray = categoryid.split(",");
								for (int i = 0; i < categoryArray.length; i++) {
									int parseInt = Integer.parseInt(categoryArray[i]);
									if(!catIds.contains(parseInt)){
										catIds.add(parseInt);
									}

								}
							}else{
								if(!catIds.contains(0)){
									catIds.add(0);
								}
							}

						}
					}

					JSONArray array = new JSONArray();
					if(catIds.size()>0){
						for(Integer id:catIds){
							JSONObject json = new JSONObject();
							if(id ==0){
								json.put("shelvesCatgegory","未分组");
							}else{
								ProductcategoryEntity productcategoryEntity = productcategoryService.getById(id);
								json.put("shelvesCatgegory",productcategoryEntity.getName());
							}

							List<ProductEntity> productlist = productService.getProductList(id,shelvesId);

							if(productlist!=null && productlist.size()>0){
								JSONArray jsonArray = new JSONArray();
								for(ProductEntity product:productlist){

									JSONObject jsonobject = new JSONObject();
									jsonobject.put("productid",product.getId());
									jsonobject.put("productimg",product.getProductimg());
									jsonobject.put("productname",product.getProductname());
									jsonobject.put("sellprice",product.getSellprice());
									jsonArray.add(jsonobject);
								}
								json.put("prodcuts",jsonArray);
							}

							array.add(json);
						}
					}

					return R.ok().put("shelvesname",goodshelves.getShelvesname()).put("data",array)
							.put("notice",takeoutsetEntity.getNotice()).put("fee",takeoutsetEntity.getFee())
							.put("sendprice",takeoutsetEntity.getSendprice()).put("totalprice",0);
				}else{
					return R.error("该货架不支持外卖");
				}

			}else{
				return R.error("所查询的货架不存在");
			}

		}else{
			return R.error("货架id不能为空");
		}

	}

	/**
	 * 补货申请
	 * @param shelvesId 货架id
	 * @param userid  申请人id
	 * @param remark  补货留言
	 * @param products 补货商品集合
	 * @return
	 */
	public R replenishment(@RequestParam("shelvesId")Integer shelvesId,@RequestParam("userid")Integer userid,
						   @RequestParam("remark")String remark,@RequestParam("products")List<Integer> products){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		if(shelvesId ==null){
			return R.error("货架id不能为空");
		}else if(userid ==null){
			return R.error("申请人id不能为空");
		}else if(products ==null||products.size()==0){
			return R.error("补货商品不能为空");
		}else{
			JSONArray array = new JSONArray();
			String number = "BH"+sdf.format(new Date());
			Date date = new Date();
			GoodshelvesEntity goodshelves = goodshelvesService.getById(shelvesId);
			ReplenishmentEntity replenishmentEntity = new ReplenishmentEntity();
			replenishmentEntity.setCount(products.size());
			replenishmentEntity.setCreatetime(date);
			replenishmentEntity.setMessage(remark);
			replenishmentEntity.setNumber(number);
			replenishmentEntity.setShelvesid(shelvesId);
			replenishmentEntity.setStatu(0);
			replenishmentService.save(replenishmentEntity);

			for(int i=0;i<products.size();i++){
				JSONObject jsonobject = new JSONObject();
				ReplenishmentitemEntity replenishmentitemEntity = new ReplenishmentitemEntity();
				replenishmentitemEntity.setProductid(products.get(i));
				replenishmentitemEntity.setReplenishid(replenishmentEntity.getId());
				replenishmentitemService.save(replenishmentitemEntity);

				ProductEntity product = productService.getById(products.get(i));
				jsonobject.put("productimg",product.getProductimg());
				jsonobject.put("productname",product.getProductname());
				array.add(jsonobject);

			}
			return R.ok().put("shelvesname",goodshelves.getShelvesname()).put("remark",remark).put("number",number).put("time",date)
					.put("products",array);
		}

	}

	/**
	 * 获取申请补货页面补货商品
	 * @return
	 */
	public R getProudct(){
		JSONArray array = new JSONArray();
		List<ProductEntity> list = productService.list(new QueryWrapper<ProductEntity>());
		if(list!=null && list.size()>0){
			for(ProductEntity product:list){
				JSONObject jsonobject = new JSONObject();
				jsonobject.put("productid",product.getId());
				jsonobject.put("productimg",product.getProductimg());
				jsonobject.put("productname",product.getProductname());
				jsonobject.put("sellprice",product.getSellprice());
				array.add(jsonobject);
			}
		}
		return R.ok().put("products",array);

	}

}
